//
//  YDAHomeBarView.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/23.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDAHomeBarView.h"

@interface YDAHomeBarView()

//客服
@property (nonatomic , weak) SYNoHeightedButton *waiterButton;
//搜索
@property (nonatomic , weak) SYNoHeightedButton *searchButton;

//搜索图片
@property (nonatomic , weak) UIImageView *searchImage;

//客服触发view
@property (nonatomic , weak) UIView *tapView;

@end

@implementation YDAHomeBarView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor clearColor];
        [self waiterButton];
        [self searchImage];
        [self tapView];
    }
    return self;
}
//客服
-(SYNoHeightedButton *)waiterButton
{
    if (_waiterButton == nil) {
        SYNoHeightedButton *waiterButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:waiterButton];
        [waiterButton setBackgroundColor:[UIColor clearColor]];
        [waiterButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        waiterButton.tag = 1;
        //h-kefu
        [waiterButton addTarget:self action:@selector(topBarButtonClick:) forControlEvents:UIControlEventTouchDown];
        
        [waiterButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.height.width.equalTo(@22);
            if (isPhoneX) {
                make.top.equalTo(self).offset(50);
            }else{
                make.top.equalTo(self).offset(30);
            }
            
        }];
        _waiterButton = waiterButton;
    }
    return _waiterButton;
}

-(void)topBarButtonClick:(SYNoHeightedButton *)button
{
//    if ([self.delegate respondsToSelector:@selector(homeTopBarView:buttonCLick:)]) {
//        [self.delegate homeTopBarView:self buttonCLick:button];
//    }
}

-(UIImageView *)searchImage
{
    if (_searchImage == nil) {
        UIImageView *searchImage = [[UIImageView alloc] init];
        [self addSubview:searchImage];
        searchImage.image = [UIImage imageNamed:@"h-search"];
        
        if (LKScreenW < 375) {
            searchImage.contentMode = UIViewContentModeScaleAspectFit;
        }else{
            searchImage.contentMode = UIViewContentModeScaleAspectFill;
        }
        
        [searchImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.waiterButton);
            make.right.equalTo(self).offset(-15);
            make.left.equalTo(self.waiterButton.mas_right).offset(10);
            make.height.equalTo(@30);
        }];
        searchImage.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [searchImage addGestureRecognizer:tap];
        _searchImage = searchImage;
    }
    return _searchImage;
}

-(void)tapClick
{
    if ([self.delegate respondsToSelector:@selector(homeTopBarViewSearchImageCLick:)]) {
        [self.delegate homeTopBarViewSearchImageCLick:self];
    }
}

-(UIView *)tapView
{
    if (_tapView == nil) {
        UIView *tapView = [[UIView alloc] init];
        [self addSubview:tapView];
        tapView.backgroundColor = [UIColor clearColor];
        tapView.userInteractionEnabled = YES;
        [tapView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self.searchImage.mas_left);
            make.height.equalTo(@44);
            make.centerY.equalTo(self.waiterButton);
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewClick)];
        [tapView addGestureRecognizer:tap];
        _tapView = tapView;
    }
    return _tapView;
}
-(void)tapViewClick
{
    if ([self.delegate respondsToSelector:@selector(homeTopBarView:buttonCLick:)]) {
        [self.delegate homeTopBarView:self buttonCLick:self.waiterButton];
    }
}


@end
